<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="80px" @submit.native.prevent>
      <el-form-item label="名称" prop="name">
        <el-input v-model="pram.name" maxlength="150" placeholder="名称" />
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input v-model="pram.url" maxlength="255" placeholder="请输入完整链接，如 https://www.xx.com" />
      </el-form-item>
      <el-form-item label="分类" prop="category_id">
        <el-select v-model="pram.category_id" placeholder="分类" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="pram.sort" :min="0"/> <span style="color: #ccc">（数字越大越靠前）</span>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="pram.status" :active-value="1" :inactive-value="0" />
      </el-form-item>
    </el-form>
    <div class="btn-box">
      <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:links:update','admin:links:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </div>
</template>

<script>
import * as linksApi from '@/api/links.js'
import * as systemSetting from '@/api/systemSetting.js'
import {getToken} from "@/utils/auth";
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCategoryData: {
      type: Array
    },
  },
  data() {
    return {
      isDisabled: this.$route.params.isDisabled==='1'?true:false,
      constants:this.$constants,
      pram: {
        name: null,
        category_id:null,
        url:null,
        sort: 0,
        status:1,
        id: 0,
      },
      rules: {
        name: [{ required: true, message: '请填标题', trigger: ['blur', 'change'] }],
        category_id: [{ required: true, message: '请选择分类', trigger: ['blur', 'change'] }],
        url: [{ required: true, message: '请填链接', trigger: ['blur', 'change'] }],
      },
      myHeaders: { "X-Token": getToken() },

      categoryList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },

    initEditData() {
      this.categoryList = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { name, url, category_id, sort, status, id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.name = name
      this.pram.url = url
      this.pram.category_id = category_id
      this.pram.status = status
      this.pram.sort = sort
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      linksApi.doAdd(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      linksApi.doAdd(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped lang="scss">
.pictrue{
  width: 150px;
  height: 150px;
  border: 1px dotted rgba(0,0,0,0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;
  img{
    width: 100%;
    height: 100%;
  }
  video{
    width: 100%;
    height: 100%;
  }
}
.rich-content{
  width:99%;
  ::v-deep.el-form-item__content{
    margin-left:10px!important;
  }
}
</style>
